package com.xinxin.intelligenceexam.studyMenu.shortcut;

import android.content.Context;
import android.content.Intent;
import android.content.pm.ShortcutInfo;
import android.content.pm.ShortcutManager;
import android.graphics.drawable.Icon;
import android.net.Uri;
import android.os.Build;
import android.widget.Toast;

import com.xinxin.intelligenceexam.R;

import java.util.Arrays;
import java.util.List;

/**
 * User: Tianxin
 * Date: 2016-11-29
 * Time: 23:41
 */

public class DynamicShortCutsUtils {

    private ShortcutManager shortcutManager;
    private Context mContext;

    public DynamicShortCutsUtils(Context context) throws Exception {
        mContext = context;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            shortcutManager = mContext.getSystemService(ShortcutManager.class);
        }else {
            throw new Exception("只能在api25以上去使用这个功能哟");
        }
    }

    private void addShortcut() {
        if (Build.VERSION.SDK_INT >= 25) {
            List<ShortcutInfo> infoList = shortcutManager.getDynamicShortcuts();
            String shortcutId = null;
            for (ShortcutInfo shortcutInfo : infoList) {
                if (ShortcutsId.WEB_DEVWIKI.equals(shortcutInfo.getId())) {
                    shortcutId = shortcutInfo.getId();
                }
            }
            if (shortcutId == null) {
                ShortcutInfo shortcut = new ShortcutInfo.Builder(mContext, ShortcutsId.WEB_DEVWIKI)
                        .setShortLabel(mContext.getString(R.string.blog_short_label))
                        .setLongLabel(mContext.getString(R.string.blog_long_label))
                        .setIcon(Icon.createWithResource(mContext, R.drawable.ic_blog_logo))
                        .setIntent(new Intent(Intent.ACTION_VIEW, Uri.parse("http://blog.devwiki.net")))
                        .build();
                shortcutManager.addDynamicShortcuts(Arrays.asList(shortcut));
                Toast.makeText(mContext, R.string.add_shortcut_success, Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(mContext, R.string.shortcut_already_exist, Toast.LENGTH_SHORT).show();
            }
        } else {
            Toast.makeText(mContext, R.string.not_support_function, Toast.LENGTH_SHORT).show();
        }
    }
    private void removeShortcut() {
        //Build.VERSION_CODES.N_MR1
        if (Build.VERSION.SDK_INT >= 25) {
            List<ShortcutInfo> infoList = shortcutManager.getDynamicShortcuts();
            String shortcutId = null;
            for (ShortcutInfo shortcutInfo : infoList) {
                if (ShortcutsId.WEB_DEVWIKI.equals(shortcutInfo.getId())) {
                    shortcutId = shortcutInfo.getId();
                }
            }
            if (shortcutId == null) {
                Toast.makeText(mContext, R.string.shortcut_not_exist, Toast.LENGTH_SHORT).show();
            } else {
                shortcutManager.removeDynamicShortcuts(Arrays.asList(shortcutId));
                Toast.makeText(mContext, R.string.remove_shortcut_success, Toast.LENGTH_SHORT).show();
            }
        } else {
            Toast.makeText(mContext, R.string.not_support_function, Toast.LENGTH_SHORT).show();
        }
    }
}
